<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. Redis 内存数据库 ######1). RedisRedis是一个开源的，高性能，C语言开发的 ，键值对(key-value)存储数据的NOSQL数据库。 ######2). 基本概念NOSQL ：Not Only Sql 泛指非关系型数据库  Redis MongoDB Hbase关系型数据库： mysql oracle sybase sqlServer">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="分布式--Redis-内存数据库">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Distributed/分布式--Redis-内存数据库/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. Redis 内存数据库 ######1). RedisRedis是一个开源的，高性能，C语言开发的 ，键值对(key-value)存储数据的NOSQL数据库。 ######2). 基本概念NOSQL ：Not Only Sql 泛指非关系型数据库  Redis MongoDB Hbase关系型数据库： mysql oracle sybase sqlServer">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-410815c86a997b8d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-42def2f1d2c8464d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-4f82541469375de1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-c458f52df58efc45.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-6eba731c134137e2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-9a65f9252f835d42.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-17549b2b06989e65.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-90ee4b43319d4ebf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-8149aedb10b3e12c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-56e60405d9201576.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-b81f84021368d159.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T11:07:11.314Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="分布式--Redis-内存数据库">
<meta name="twitter:description" content="####1. Redis 内存数据库 ######1). RedisRedis是一个开源的，高性能，C语言开发的 ，键值对(key-value)存储数据的NOSQL数据库。 ######2). 基本概念NOSQL ：Not Only Sql 泛指非关系型数据库  Redis MongoDB Hbase关系型数据库： mysql oracle sybase sqlServer">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-410815c86a997b8d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Distributed/分布式--Redis-内存数据库/"/>





  <title>分布式--Redis-内存数据库 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Distributed/分布式--Redis-内存数据库/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">分布式--Redis-内存数据库</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. <a href="https://redis.io/" target="_blank" rel="noopener">Redis 内存数据库</a></p>
<p>######1). Redis<br>Redis是一个开源的，高性能，C语言开发的 ，键值对(key-value)存储数据的NOSQL数据库。</p>
<p>######2). 基本概念<br>NOSQL ：Not Only Sql 泛指非关系型数据库  Redis MongoDB Hbase<br>关系型数据库： mysql oracle sybase sqlServer<br><a id="more"></a></p>
<p>######3). 数据库应用的发展历史 </p>
<ul>
<li>1：在互联网时代来临之前 ，企业的一些内部信息管理系统，一个单个的数据库的实例就可以应付系统的功能需求<br><strong>单数据库实例</strong></li>
<li>2：随着系统访问用户的增多 ，数据量的增大，单个数据库的实例已经满足不了系统频繁读取数据（权限的验证，系统的默认配置）的需求 。<br><strong>缓存 （ehCache/MemCached）+ 数据库实例</strong></li>
<li>3: 缓存虽然可以缓解数据库的读取压力 ，但是数据量的写入压力持续增大 ，这个时候我们需要采取数据库主从机制进行读写分离<br><strong>缓存 （ehCache/MemCached）+ 主从数据库 + 读写分离</strong></li>
<li>4： 数据量再次增大，读写分离以后，主数据库的写库的压力出现瓶颈<br><strong>缓存 （ehCache/MemCached）+ 主从数据库 + 读写分离 +分库分表</strong></li>
<li>5：互联网和大数据的时代的来林，关系型数据库已经不能很好的存储一些并发性大，实时性高，而且格式不固定的数据<br><strong>Nosql数据库+主从数据库 + 读写分离 +分库分表</strong><br>######4). 系统架构中的位置<br><img src="https://upload-images.jianshu.io/upload_images/3110861-410815c86a997b8d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"></li>
</ul>
<p>####2. <a href="https://www.jianshu.com/p/044d2e599a42" target="_blank" rel="noopener">安装</a></p>
<p>####3. Redis的五大数据类型以及应用<br><strong>String/Hash/List/Set/sorted set</strong></p>
<ul>
<li>字符串类型 String</li>
<li>列表类型    list</li>
<li>集合类型    set</li>
<li>有序集合类型    sorted set</li>
<li>散列类型    hash<br>######1). K-V格式中 V是一个字符串对象<br>set k1 v1<br>get k1<br>del k1 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">incr num (自增1 如果没有设置值默认为1)</span><br><span class="line">decr num (自减1)</span><br><span class="line">incrby num 5	(自增5)</span><br><span class="line">decrby num 5	(自减 5)</span><br><span class="line">Append num 4  (追加4)返回字符串长度位数,不是数学+ 是拼接</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>应用场景： 秒杀商品/商品点赞</p>
<p>######2). Hash<br>K-V格式中 V是一个Hash类型 其实java里面的Map&lt;String,object&gt;</p>
<ul>
<li><p>赋值命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">hset student id 1</span><br><span class="line">hset student name mazaiting</span><br><span class="line">hset student sex boy</span><br><span class="line">hmset myhash username admin age 20</span><br></pre></td></tr></table></figure>
</li>
<li><p>取值命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">hget myhash username</span><br><span class="line">hget myhash username age</span><br><span class="line">hgetall myhash</span><br></pre></td></tr></table></figure>
</li>
<li><p>删除命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">hdel myhash username age</span><br><span class="line">del myhash	(删除myhash)</span><br><span class="line">hincrby myhash age 5	(增加5)</span><br></pre></td></tr></table></figure>
</li>
<li><p>其他命名</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">hexists myhash username 判断myhash中是否包含username包含返回1否则返回0</span><br><span class="line">hlen myhash 获取myhash中的字段数</span><br><span class="line">hkeys myhash 获取所有的 key</span><br><span class="line">hvals myhash 获取所有的值</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>应用场景： 电商网站的商品详细信息</p>
<p>######3). list<br>K-V格式中 V是一个list类型 其实java里面的LinkedList</p>
<p>把他看成一个竹子，两边是打通了<br>从左插入记录  lpush book  1 2 3<br>从右插入记录  rpush book  4 5 6<br>从左弹出记录  lpop book 1<br>从右弹出记录  lpush book 1<br>列表里面的记录顺序（竹子）– 3  2  1  4  5  6 </p>
<p>应用场景： 商品的评论</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">lpush mylist 1 2 3	从左插入记录</span><br><span class="line">rpush mylist a b c 从右插入记录</span><br><span class="line"></span><br><span class="line">lrange mylist 开始索引位置 结束索引位置(-1最后一个)</span><br><span class="line">lpop mylist 左侧弹出（相当于删除）</span><br><span class="line">rpop mylist 右侧弹出（相当于删除）</span><br><span class="line">lpush mylist 左侧插入</span><br><span class="line">rpush mylist 右侧插入</span><br><span class="line">lrem mylist 删除几个元素 删除哪个元素</span><br><span class="line">lset mylist 索引位置 插入的值</span><br><span class="line">linsert mylist before 哪个值前面 要插入的值</span><br><span class="line">linsert mylist after 哪个值后面 要插入的值</span><br><span class="line">rpoplpush 集合1 集合2  把集合1中取出的值放在集合2中</span><br></pre></td></tr></table></figure>
<p>######4). Set<br>K-V格式中 V是一个set类型  set是无序的/set里面的元素具有唯一性、<br>sadd summer_friends sky big mo mike<br>sadd maybe_friends sky big tdd world oky<br>应用场景： 独立IP进行网站投票 /微信朋友圈的共同好友<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">sadd myset a b c 添加 a b c</span><br><span class="line">不能添加重复的值</span><br><span class="line">srem myset a c 移除a c</span><br><span class="line"></span><br><span class="line">smembers myset 查看所有值</span><br><span class="line">sismember myset a 查看集合里面是否包含a包含返回1 否则返回0</span><br><span class="line"></span><br><span class="line">sadd myset2 a b c </span><br><span class="line">sadd myset3 a c 1 </span><br><span class="line">sdiff myset2 myset3	 myset2 和myset3相差的是b  差集运算</span><br><span class="line">sinter myset2 myset3	 myset2 和myset3相交的是a c  交集运算</span><br><span class="line">sunion myset2 myset3 	并集运算		合并集合 相同的会去掉(值不能重复)</span><br><span class="line">scard myset 获取集合数量</span><br><span class="line">srandmember myset 随机获取集合中的一个值</span><br><span class="line">sdiffstore myset4 myset2 myset3 		将myset2 和myset3中相差的值放到myset4里面</span><br><span class="line">sinterstore myset4 myset2 myset3		将myset2 和myset3中相交的值放到myset4里面</span><br><span class="line">sunionstore myset4 myset2 myset3	将myset2 和myset3中相并的值放到myset4里面</span><br></pre></td></tr></table></figure></p>
<p>######5). sorted-set<br>游戏排名 热点话题中应用<br>K-V格式中 V是一个Zset类型  zset是有序的? 怎么做到有序</p>
<p>应用场景: 商品的销售排行榜<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">zadd sales  100  huawei  99 iphone  98 vivo  97 oppo</span><br><span class="line">Zrange sales 0 -1 withscores  从小到大</span><br><span class="line">Zrevrange sales 0 -1 withscores  从大到小</span><br></pre></td></tr></table></figure></p>
<p>####4. Java 使用Redis</p>
<p>######1).  工具下载<br><a href="https://mvnrepository.com/artifact/redis.clients/jedis" target="_blank" rel="noopener">Jedis下载</a><br>此处使用windows下的redis工具包，因为虚拟机连接会出现很多问题。</p>
<p>######1). <a href="https://github.com/MSOpenTech/redis/releases" target="_blank" rel="noopener">Redis-win下载</a><br>下载后解压，如下图<br><img src="https://upload-images.jianshu.io/upload_images/3110861-42def2f1d2c8464d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"></p>
<p>######2). 在上方地址栏输入<code>cmd</code>然后回车，这时会打开命令提示符窗口，然后输入<code>redis-server.exe redis.windows.conf</code> 开启redis服务器.<br><img src="https://upload-images.jianshu.io/upload_images/3110861-4f82541469375de1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"></p>
<p>######3). 在上方地址栏继续输入<code>cmd</code>回车，输入命令<code></code>打开redis客户端<br><img src="https://upload-images.jianshu.io/upload_images/3110861-c458f52df58efc45.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"></p>
<p>######2). 连接<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">public class RedisJava &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		Jedis jedis = new Jedis(&quot;127.0.0.1&quot;, 6379);</span><br><span class="line">		System.out.println(&quot;服务正在运行： &quot; + jedis.ping());</span><br><span class="line">		jedis.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######3). 测试<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">public class RedisJava &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		Jedis jedis = new Jedis(&quot;127.0.0.1&quot;, 6379);</span><br><span class="line">		System.out.println(&quot;服务正在运行： &quot; + jedis.ping());</span><br><span class="line">		jedis.set(&quot;name&quot;, &quot;linghaoyu&quot;);</span><br><span class="line">		System.out.println(jedis.get(&quot;name&quot;));;</span><br><span class="line">		jedis.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-6eba731c134137e2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"></p>
<p>####5. 其他</p>
<p>######1). Redis HyperLogLog命令<br>Redis HyperLogLog 是用来做基数统计的算法，HyperLogLog 的优点是，在输入元素的数量或者体积非常非常大时，计算基数所需的空间总是固定 的、并且是很小的。<br><strong>基数：</strong>数据集 {1, 3, 5, 7, 5, 7, 8}， 那么这个数据集的基数集为 {1, 3, 5 ,7, 8}, 基数(不重复元素)为5。 基数估计就是在误差可接受的范围内，快速计算基数。<br><img src="https://upload-images.jianshu.io/upload_images/3110861-9a65f9252f835d42.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图7.png"></p>
<p>######2). Redis 订阅<br>I. Redis 发布订阅(pub/sub)是一种消息通信模式：发送者(pub)发送消息，订阅者(sub)接收消息。Redis 客户端可以订阅任意数量的频道。<br>下图展示了频道 channel1 ， 以及订阅这个频道的三个客户端 —— client2 、 client5 和 client1 之间的关系：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-17549b2b06989e65.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图8.png"><br>当有新消息通过 PUBLISH 命令发送给频道 channel1 时， 这个消息就会被发送给订阅它的三个客户端：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-90ee4b43319d4ebf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图9.png"></p>
<p>II. 实例</p>
<ul>
<li>订阅<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">subscribe redisChat</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-8149aedb10b3e12c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图10.png"></p>
<ul>
<li>发布消息<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">publish redisChat &quot;Redis is a great caching technique&quot;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-56e60405d9201576.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图11.png"></p>
<p>######3). Redis 事务<br>Redis 事务可以一次执行多个命令， 并且带有以下两个重要的保证：</p>
<ul>
<li>事务是一个单独的隔离操作：事务中的所有命令都会序列化、按顺序地执行。事务在执行的过程中，不会被其他客户端发送来的命令请求所打断。</li>
<li>事务是一个原子操作：事务中的命令要么全部被执行，要么全部都不执行。</li>
</ul>
<p>一个事务从开始到执行会经历以下三个阶段：</p>
<ul>
<li>开始事务。</li>
<li>命令入队。</li>
<li>执行事务。</li>
</ul>
<p>示例：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; multi</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; set book-name &quot;Mastering C++ in 21 days&quot;</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379&gt; get book-name</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379&gt; sadd tag &quot;C++&quot; &quot;Programming&quot; &quot;Matering Series&quot;</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379&gt; smembers tag</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379&gt; exec</span><br><span class="line">1) OK</span><br><span class="line">2) &quot;Mastering C++ in 21 days&quot;</span><br><span class="line">3) (integer) 3</span><br><span class="line">4) 1) &quot;Matering Series&quot;</span><br><span class="line">   2) &quot;C++&quot;</span><br><span class="line">   3) &quot;Programming&quot;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-b81f84021368d159.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图12.png"></p>
<table>
<thead>
<tr>
<th>序号</th>
<th>命令及描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>DISCARD 取消事务，放弃执行事务块内的所有命令。</td>
</tr>
<tr>
<td>2</td>
<td>EXEC 执行所有事务块内的命令。</td>
</tr>
<tr>
<td>3</td>
<td>MULTI 标记一个事务块的开始。</td>
</tr>
</tbody>
</table>
<p>######4). <a href="https://redisdesktop.com/download" target="_blank" rel="noopener">Redis 桌面管理</a></p>
<p>####<a href="https://gitee.com/mazaiting/distributed" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Distributed/分布式--NIO-Netty/" rel="next" title="分布式--NIO-Netty">
                <i class="fa fa-chevron-left"></i> 分布式--NIO-Netty
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Distributed/分布式--Solr-站内搜索引擎/" rel="prev" title="分布式--Solr-站内搜索引擎">
                分布式--Solr-站内搜索引擎 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
